from . import admins_bp
from .forms.article import EditForm, SearchForm
from ...helper import redirect_back
from ...models import Article, Tag
from flask import render_template, flash, request, redirect, url_for
from random import randint
from bs4 import BeautifulSoup


@admins_bp.route('/article')
def article_select():
    query = Article.query
    category = request.args.get('category_id', type=int, default=0)
    status = request.args.get('status', type=int)
    if category:
        query = query.filter_by(category_id=category)
    title = request.args.get('title', type=str, default='')
    if title:
        query = query.filter(Article.title.like('%{}%'.format(title)))
    if request.args.get('check'):
        query = query.filter(Article.status==False)
    article = query.order_by(Article.id.desc()).paginate()
    return render_template(
        'article_select.html',
        record=article,
        page_title='文章信息',
        form=SearchForm(csrf_enabled=False, **request.args)
    )


@admins_bp.route('/article/<int:article_id>/modify', methods=['GET', 'POST'])
def article_modify(article_id):
    article = Article.query.get_or_404(article_id)
    form = EditForm()
    if form.validate_on_submit():
        if not form.description.data:
            soup = BeautifulSoup(form.content.data, "html.parser")
            for script in soup.findAll('script'):
                script.extract()
            for style in soup.findAll('style'):
                style.extract()
            desc = soup.get_text()[0:120]
            form.description.data = desc
        article.update(form.data)
        flash('更新成功', 'success')
        return redirect_back('admins.article_select')
    form.title.data = article.title
    form.original_title.data = article.original_title
    form.thumbnail.data = article.thumbnail
    form.content.data = article.body.content
    form.description.data = article.description
    form.category_id.data = article.category_id
    form.source.data = article.source
    form.tags.data = article.tags_str
    form.hot.data = article.hot
    form.top.data = article.top
    form.focus.data = article.focus
    form.good.data = article.good
    form.author.data = article.author
    return render_template(
        'article_edit.html',
        page_title='文章编辑',
        form=form,
        jump_for_up=request.args.get('next')
    )


@admins_bp.route('/article/create', methods=['GET', 'POST'])
def article_create():
    form = EditForm()
    if form.validate_on_submit():
        if not form.description.data:
            soup = BeautifulSoup(form.content.data, "html.parser")
            for script in soup.findAll('script'):
                script.extract()
            for style in soup.findAll('style'):
                style.extract()
            desc = soup.get_text()[0:120]
            form.description.data = desc
        Article.admins_add(form.data)
        flash('新增成功', 'success')
        return redirect(url_for('admins.article_select'))
    form.hot.data = randint(100, 2000)
    return render_template(
        'article_edit.html',
        page_title='文章编辑',
        form=form
    )


@admins_bp.route('/article/get_tags')
def article_pull_tags():
    query = Tag.query
    args = request.args
    if args.get('title', type=str):
        query = query.filter(Tag.tag_name.like('%{}%'.format(args.get('title'))))
    if args.get('good'):
        query = query.filter_by(good=1)
    _tags = query.paginate()
    return render_template('article_pull_tags.html', record=_tags)
